//
//  OrderImageView.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/1.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

protocol OrderImageViewDelegate {
    func deleteImage(orderImageView: OrderImageView, deleted: String)
    func showDetailImage(image: UIImage)
}

class OrderImageView: UIView {
    
    let defaultImage = UIImage(named: "loading_reco")
    
    var delegate: OrderImageViewDelegate?
    
    var filename = ""
    
    var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    var deleteBtn: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "delete_date"), for: .normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        let tapGesture = UITapGestureRecognizer()
        tapGesture.addTarget(self, action: #selector(tapImageView(_:)))
        self.addGestureRecognizer(tapGesture)
        self.addSubview(imageView)
        self.addSubview(deleteBtn)
        deleteBtn.addTarget(self, action: #selector(pressBtn(_:)), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initValue(url: String, isEditable: Bool) {
        if url != "" {
            let url_array = url.components(separatedBy: "/")
            filename = url_array[url_array.count - 1]
            let url = URL(string: url)
            self.imageView.kf.setImage(with: url, placeholder: defaultImage)
        }
        if !isEditable {
            self.deleteBtn.removeFromSuperview()
        }
    }
    
    func initValue(image: UIImage, isEditable: Bool) {
        self.imageView.image = image
        self.filename = "new"
        if !isEditable {
            self.deleteBtn.removeFromSuperview()
        }
    }
    
    func tapImageView(_ sender: UITapGestureRecognizer) {
        if let image = self.imageView.image {
            self.delegate?.showDetailImage(image: image)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let btnWidth: CGFloat = self.bounds.width / 4
        imageView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(btnWidth , btnWidth / 2, 0, btnWidth / 2))
        deleteBtn.frame = CGRect(x: self.bounds.width - btnWidth, y: 0, width: btnWidth, height: btnWidth)
    }
    
    func pressBtn(_ sender: UIButton) {
        self.delegate?.deleteImage(orderImageView: self, deleted: filename)
    }
}

class OrderAddImageView: UIView {
    
    var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.image = UIImage(named: "addImage")
        return imageView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        self.layer.cornerRadius = 15
        self.layer.masksToBounds = true
        self.layer.borderWidth = 3
        self.layer.borderColor = addImageButtonColor.cgColor
        self.addSubview(imageView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        let ivWidth = self.bounds.width * 2 / 3
        imageView.frame = CGRect(x: (bounds.width - ivWidth) / 2, y: (bounds.width - ivWidth) / 2, width: ivWidth, height: ivWidth)
    }
}
